/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jefb.service.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.jefb.annotations.LazyInit;
import org.jefb.dao.ICommonDao;
import org.jefb.entity.RoleEntity;
import org.jefb.sec.entity.UserRight;
import org.jefb.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author alexej
 * 
 */
@Service
public class RoleService implements IRoleService {

	@Autowired
	private ICommonDao commonDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jefb.service.IRoleService#getPermissions(org.jefb.entity.RoleEntity)
	 */
	// @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
	// public Set<UserRight> getPermissions(RoleEntity entity) {
	// Set<UserRight> permissions = new HashSet<UserRight>();
	// commonDAO.refresh(entity);
	// Collection<UserRight> collection = getCollection(entity,
	// UserRight.class);
	// Iterator<UserRight> iterator = collection.iterator();
	// while(iterator.hasNext()){
	// permissions.add(iterator.next());
	// }
	// return permissions;
	// }

	// @SuppressWarnings("unchecked")
	// @Transactional(propagation = Propagation.REQUIRED)
	// public <T,CC> Collection<CC> getCollection(T entity, Class<CC>
	// collectionClazz) {
	// Class<? extends Object> clazz = entity.getClass();
	// Method[] methods = clazz.getMethods();
	// for(Method method:methods){
	// Annotation[] annotations = method.getAnnotations();
	// for(Annotation annotation:annotations){
	// if (annotation instanceof LazyInit) {
	// LazyInit lazyInitAnnotation = (LazyInit) annotation;
	// if(lazyInitAnnotation.value().equals(collectionClazz)){
	// try {
	// Collection<CC> lazyCollection = (Collection<CC>)method.invoke(entity, new
	// Object[]{});
	// return lazyCollection;
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// }
	// }
	// }
	// return Collections.EMPTY_LIST;
	// }

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jefb.service.IRoleService#getAssignedPermissions(org.jefb.entity.
	 * RoleEntity)
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public List<UserRight> getUnassignedPermissions(RoleEntity entity) {
		List<UserRight> unassignedPermissions = new ArrayList<UserRight>();
		unassignedPermissions.addAll(Arrays.asList(UserRight.values()));
		Collection<UserRight> rights = entity.getRights();
		if (rights != null && !rights.isEmpty()) {
			for (UserRight right : rights) {
				if (unassignedPermissions.contains(right)) {
					unassignedPermissions.remove(right);
				}
			}
		}
		return unassignedPermissions;
	}

	public void setCommonDAO(ICommonDao commonDAO) {
		this.commonDAO = commonDAO;
	}

	public ICommonDao getCommonDAO() {
		return commonDAO;
	}

}
